package gl.android.biyiqing.fragment;

import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.android.biyiqing.bean.BYQUserInfo;
import gl.android.biyiqing.competent.ShakeListener;
import gl.android.biyiqing.competent.ShakeListener.OnShakeListener;
import gl.common.AjaxCallBackProxy;
import gl.common.BaseControl;
import gl.common.UserManger;
import gl.uitls.Tip;

import org.json.JSONObject;

import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class YaoYiYaoFragment extends BaseListFragmet {
	// Content View Elements

	// Content View Elements

	// Content View Elements

	private TextView empty;
	private ListView list;
	private Animation animationShark;
	// End Of Content View Elements
	private ShakeListener mShakeListener;
	private SoundPool sndPool;
	String NOTFOUND = "此刻没有找到\n与你一起摇一摇的人。\n再试一次吧！";
	private Drawable drawable;

	private void bindViews() {

		empty = (TextView) findViewById(R.id.empty);
		list = (ListView) findViewById(android.R.id.list);
		list.setEmptyView(empty);
		drawable = getResources().getDrawable(R.drawable.ic_discovery_pic);
		empty.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
				null);
		
		empty.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				sendShark();
				startShakeAnima();
			}
		});

	}

	public static YaoYiYaoFragment newInstance() {
		YaoYiYaoFragment fragment = new YaoYiYaoFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}

	public YaoYiYaoFragment() {
	}

	private SparseIntArray soundPoolMap = new SparseIntArray();

	private void loadSound() {

		sndPool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 5);
		new Thread() {
			public void run() {
				soundPoolMap
						.put(0, sndPool.load(getActivity(), R.raw.knock, 1));

				soundPoolMap.put(1,
						sndPool.load(getActivity(), R.raw.page_flip_short, 1));
			}
		}.start();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mShakeListener != null) {
			mShakeListener.stop();
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.listview_empty, container, false);
		bindViews();
		animationShark = AnimationUtils.loadAnimation(getActivity(),
				R.anim.shake);
		animationShark.setRepeatCount(20);
		loadSound();
		mShakeListener = new ShakeListener(getActivity());
		mShakeListener.setOnShakeListener(new OnShakeListener() {
			public void onShake() {
				// Toast.makeText(getApplicationContext(),
				// "抱歉，暂时没有找到在同一时刻摇一摇的人。\n再试一次吧！", Toast.LENGTH_SHORT).show();
				shark();
			}
		});
		getListView().setAdapter(new BaseAdapter() {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				return new ImageView(getActivity());
			}
			
			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return 0;
			}
			
			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return 1;
			}
		});
		empty.setVisibility(View.VISIBLE);
		return rootView;
	}

	private void getShark() {
		// jobs: 0,
		// username: "平凡26",
		// height: 172,
		// age: "0025",
		// userid: 162012,
		// groupid: 1,
		// education: 4,
		// avatar:
		// "data/attachment/avatar/201301/22/162012/avatar_big.jpg.thumb.jpg",
		// online: 1
		initListView(getListView(), API.URL + "GetSharkList?userid=%s",
				new Object[] { UserManger.getInstance().userid },
				R.layout.item_list_image, new String[] { "username", "avatar",
			BYQUserInfo.KEY_SHENFEN,BYQUserInfo.KEY_GROUP,BYQUserInfo.KEY_VIP_UNIT }, new int[] { R.id.tv_nick, R.id.iv_avator,
			R.id.age,R.id.iv_vip,R.id.imageView1 , R.id.iv_fav, R.id.iv_sendmsg });

	}
	@Override
	public void onResume() {
		super.onResume();
	}
	private void initListView(final AbsListView adpateview, String url,
			Object[] pars, final int itemLayout, final String[] from,
			final int[] to) {
		BaseControl.get(url, new AjaxCallBackProxy() {
			void finily() {
				onSharkEnd();
			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				if (!isValueFragment()) {
					return;
				}
				finily();
				Tip.show(NOTFOUND);
			}

			@Override
			public void onSuccess(String t) {
				if (!isValueFragment()) {
					return;
				}
				super.onSuccess(t);
				try {
					
					animationShark.cancel();
					if (new JSONObject(t).getJSONArray("data") == null||new JSONObject(t).getJSONArray("data").length()<=0) {
						Tip.show(NOTFOUND);
						return;
					}
					dataJSONArray = new JSONObject(t).getJSONArray("data") ;
					
					bindDataToAbsListView(adpateview, itemLayout, from, to, t);
					
				} catch (Exception e) {
					e.printStackTrace();
					onFailure(e, 0, e.getMessage());
				}finally{
					finily();
				}

			}
		}, pars);
	}

	private void sendShark() {
		BaseControl.get(API.URL + "SetShark?userid=%s",
				new AjaxCallBackProxy() {
					@Override
					public void onSuccess(String t) {
						if (!isValueFragment()) {
							return;
						}
						super.onSuccess(t);
						getShark();
					}
				}, new Object[] { UserManger.getInstance().userid });
	}

	private void startShakeAnima() {
		empty.startAnimation(animationShark);
	}

	private void shark() {
		startShakeAnima(); // 开始 摇一摇动画
		mShakeListener.stop();
		sndPool.play(soundPoolMap.get(0), (float) 1, (float) 1, 0, 0,
				(float) 1.2);
		sendShark();
	}

	private void onSharkEnd() {
		sndPool.play(soundPoolMap.get(1), (float) 1, (float) 1, 0, 0,
				(float) 1.0);
		mShakeListener.start();
		animationShark.cancel();
	}
	@Override
	protected String preFormatJsonData(String t) {
		// TODO Auto-generated method stub
		return BYQUserInfo.preFormatShenFen(t);
	}
}
